package com.net.advancedThread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class BlockingQueueTest {

	public static void main(String[] args)
	{
		BlockingQueue sharedQueue = new LinkedBlockingQueue();
		
		Thread t1 = new Thread(new Producer(sharedQueue));
		
		Thread t2 = new Thread(new Consumer(sharedQueue));
		
		t1.start();
		t2.start();
		

	}
	
	
	
	

}


class Producer implements Runnable
{
	private final BlockingQueue sharedQueue;
	
	Producer(BlockingQueue sharedQueue)
	{
		this.sharedQueue = sharedQueue;
	}
	
	

	@Override
	public void run()
	{
		for(int i=0;i<=10;i++)
		{
			System.out.println("produce   " +i);
			try {
				sharedQueue.put(i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}

class Consumer implements Runnable
{
   private final BlockingQueue sharedQueue;
    
    Consumer(BlockingQueue sharedQueue)
    {
    	this.sharedQueue = sharedQueue;
    }
	
	@Override
	public void run() 
	{
		while(true)
		{
			try {
				System.out.println("consume  " +sharedQueue.take());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
